import { $get, $post, $put, $delete } from '@js/authRequest'
import { compressLicenseAndMobile, decomporessListLicenseAndMobileAndBank } from '@src/assets/js/tool'

// 获取字典信息
export async function getInfoListByName (name, type) {
  const res = await $get('/api/xapc/party/getByName', {
    name,
    subject_type: type
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  decomporessListLicenseAndMobileAndBank(res.data)
  return res.data
}

// 根据完整名称获取企业信息
export async function getDataByFullName (name, type) {
  const res = await $get('/api/xapc/party/getByFullName', {
    name,
    subject_type: type
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  decomporessListLicenseAndMobileAndBank(res.data)
  return res.data instanceof Array && res.data.length > 0 ? res.data[0] : null
}

// 添加当事人
export async function addPartyRequest (data) {
  const license = data.license
  compressLicenseAndMobile(data)
  const res = await $post('/api/xapc/party', data)
  data.license = license
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return {
    ...data,
    id: res.data
  }
}

// 更新当事人
export async function updatePartyRequest (data) {
  const license = data.license
  compressLicenseAndMobile(data)
  const o = JSON.parse(JSON.stringify(data))
  const res = await $put(`/api/xapc/party/${data.id}`, o)
  data.license = license
  if (res.status !== 1) {
    throw new Error(res.message)
  }

  return data
}

// 删除当事人
export async function deletePartyRequest (id) {
  const res = await $delete(`/api/xapc/party/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return id
}
